﻿using System;
using System.Data;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseData;
using BaseData.UI;
using TaxBiz.SysConfig;
using TaxBiz.User;
using TaxEntity.User;

namespace Admin.User
{
    public partial class ModifyUser : BaseCommon.BasePage
    {
        protected string id = string.Empty;
        protected UserEntity ue = new UserEntity();
        protected UserBiz ub = new UserBiz();
        protected DataTable dtDepartment = null;
        protected DataTable dtGangwei = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!HasPower())
            {
                Jscript.AlertAndGoBack("您权限不足，不能操作");
                return;
            }
            string action = PageContext.GetRequest("action");
            id = PageContext.GetRequest("id");
            if (string.IsNullOrEmpty(id))
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            int iId = 0;
            int.TryParse(id, out iId);
            if (iId <= 0)
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            ue = ub.GetEntityById(iId);
            if (ue == null)
            {
                Jscript.AlertAndGoBack("参数错误");
                return;
            }
            if (!string.IsNullOrEmpty(action))
            {
                this.DoAction(action, iId);
            }
            else
            {
                dtDepartment = new DepartmentBiz().GetList();
                BindSelect(this.selDepart, dtDepartment, "name", "id", false, ue.DepartmentID.ToString());
                dtGangwei = PowerInfoBiz.GetGangweiList();
                BindSelect(this.selGangwei, dtGangwei, "typename", "usertype", false, ue.UserType.ToString());
            }
        }

        private bool DoAction(string action, int id)
        {
            UserEntity ue = new UserEntity();
            ue.UserId = id;
            //检查用户名
            //string userName = PageContext.GetRequest("txtUserName");
            //if (string.IsNullOrEmpty(userName))
            //{
            //    Jscript.Alert("用户名不能为空");
            //    return false;
            //}
            //if (userName.Length > 20)
            //{
            //    Jscript.Alert("用户名长度过长");
            //    return false;
            //}
            //if (db.Exists(userName))
            //{
            //    Jscript.Alert("用户名已存在，不能重复添加");
            //    return false;
            //}
            //ue.UserName = userName;
            //检查密码
            string password = PageContext.GetRequest("txtPassword");
            //if (string.IsNullOrEmpty(password))
            //{
            //    Jscript.Alert("密码不能为空");
            //    return false;
            //}
            if (!string.IsNullOrEmpty(password))
            {
                ue.Pwd = ub.GetEncryptPassword(password);
            }
            //其它参数
            ue.SName = PageContext.GetRequest("txtRealName", 10);
            ue.DepartmentID = int.Parse(this.selDepart.Value);
            ue.UserType = int.Parse(this.selGangwei.Value);
            ue.EMail = PageContext.GetRequest("txtEmail", 50);

            ue.OptUserID = UserLogin.UserId;
            bool isSucc = ub.Update(ue, UserLogin);
            if (!isSucc)
            {
                Jscript.Alert("修改失败");
                return false;
            }

            Jscript.AlertAndRedirect("修改成功", "ModifyUser.aspx?id=" + id);
            return true;
        }
    }
}